package com.example.sentinel;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

@Configuration
public class SentinelConfig {

	@Bean
	SentinelResourceAspect sentinelResourceAspect() {
		return new SentinelResourceAspect();
	}

	@PostConstruct
	public void initRules() {
		List<FlowRule> rules = new ArrayList<>();

		// 接口A的限流规则 - QPS=2
		FlowRule ruleA = new FlowRule();
		ruleA.setResource("apiA");
		ruleA.setGrade(RuleConstant.FLOW_GRADE_QPS);
		ruleA.setCount(2); // 每秒最多2次请求
		rules.add(ruleA);

		// 接口B的限流规则 - QPS=5
		FlowRule ruleB = new FlowRule();
		ruleB.setResource("apiB");
		ruleB.setGrade(RuleConstant.FLOW_GRADE_QPS);
		ruleB.setCount(5); // 每秒最多5次请求
		rules.add(ruleB);

		FlowRuleManager.loadRules(rules);
	}
}